home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlop.1 < prev    next >
Text File  |  1995-07-25  |  64KB  |  1,453 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlop - Perl operators and precedence
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.           Perl operators have the following associativity and
  13.           precedence, listed from highest precedence to lowest.  Note
  14.           that all operators borrowed from C keep the same precedence
  15.           relationship with each other, even where C's precedence is
  16.           slightly screwy.  (This makes learning Perl easier for C
  17.           folks.)
  18.  
  19.               left        terms and list operators (leftward)
  20.               left        ->
  21.               nonassoc    ++ --
  22.               right       **
  23.               right       ! ~ \ and unary + and -
  24.               left        =~ !~
  25.               left        * / % x
  26.               left        + - .
  27.               left        << >>
  28.               nonassoc    named unary operators
  29.               nonassoc    < > <= >= lt gt le ge
  30.               nonassoc    == != <=> eq ne cmp
  31.               left        &
  32.               left        | ^
  33.               left        &&
  34.               left        ||
  35.               nonassoc    ..
  36.               right       ?:
  37.               right       = += -= *= etc.
  38.               left        , =>
  39.               nonassoc    list operators (rightward)
  40.               left        not
  41.               left        and
  42.               left        or xor
  43.  
  44.           In the following sections, these operators are covered in
  45.           precedence order.
  46.  
  47.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNNSSSS
  48.           TTTTeeeerrrrmmmmssss aaaannnndddd LLLLiiiisssstttt OOOOppppeeeerrrraaaattttoooorrrrssss ((((LLLLeeeeffffttttwwwwaaaarrrrdddd))))
  49.  
  50.           Any TERM is of highest precedence of Perl.  These includes
  51.           variables, quote and quotelike operators, any expression in
  52.           parentheses, and any function whose arguments are
  53.           parenthesized.  Actually, there aren't really functions in
  54.           this sense, just list operators and unary operators behaving
  55.           as functions because you put parentheses around the
  56.           arguments.  These are all documented in the _p_e_r_l_f_u_n_c
  57.           manpage.
  58.  
  59.           If any list operator (_p_r_i_n_t(), etc.) or any unary operator
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  71.  
  72.  
  73.  
  74.           (_c_h_d_i_r(), etc.) is followed by a left parenthesis as the
  75.           next token, the operator and arguments within parentheses
  76.           are taken to be of highest precedence, just like a normal
  77.           function call.
  78.  
  79.           In the absence of parentheses, the precedence of list
  80.           operators such as print, sort, or chmod is either very high
  81.           or very low depending on whether you look at the left side
  82.           of operator or the right side of it.  For example, in
  83.  
  84.               @ary = (1, 3, sort 4, 2);
  85.               print @ary;         # prints 1324
  86.  
  87.           the commas on the right of the sort are evaluated before the
  88.           sort, but the commas on the left are evaluated after.  In
  89.           other words, list operators tend to gobble up all the
  90.           arguments that follow them, and then act like a simple TERM
  91.           with regard to the preceding expression.  Note that you have
  92.           to be careful with parens:
  93.  
  94.               # These evaluate exit before doing the print:
  95.               print($foo, exit);  # Obviously not what you want.
  96.               print $foo, exit;   # Nor is this.
  97.  
  98.               # These do the print before evaluating exit:
  99.               (print $foo), exit; # This is what you want.
  100.               print($foo), exit;  # Or this.
  101.               print ($foo), exit; # Or even this.
  102.  
  103.           Also note that
  104.  
  105.               print ($foo & 255) + 1, "\n";
  106.  
  107.           probably doesn't do what you expect at first glance.  See
  108.           the section on _N_a_m_e_d _U_n_a_r_y _O_p_e_r_a_t_o_r_s for more discussion of
  109.           this.
  110.  
  111.           Also parsed as terms are the do {} and eval {} constructs,
  112.           as well as subroutine and method calls, and the anonymous
  113.           constructors [] and {}.
  114.  
  115.           See also the section on _Q_u_o_t_e _a_n_d _Q_u_o_t_e_l_i_k_e _O_p_e_r_a_t_o_r_s toward
  116.           the end of this section, as well as the section on _I/_O
  117.           _O_p_e_r_a_t_o_r_s.
  118.  
  119.           TTTThhhheeee AAAArrrrrrrroooowwww OOOOppppeeeerrrraaaattttoooorrrr
  120.  
  121.           Just as in C and C++, "->" is an infix dereference operator.
  122.           If the right side is either a [...] or {...} subscript, then
  123.           the left side must be either a hard or symbolic reference to
  124.           an array or hash (or a location capable of holding a hard
  125.           reference, if it's an lvalue (assignable)).  See the _p_e_r_l_r_e_f
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  137.  
  138.  
  139.  
  140.           manpage.
  141.  
  142.           Otherwise, the right side is a method name or a simple
  143.           scalar variable containing the method name, and the left
  144.           side must either be an object (a blessed reference) or a
  145.           class name (that is, a package name).  See the _p_e_r_l_o_b_j
  146.           manpage.
  147.  
  148.           AAAAuuuuttttooooiiiinnnnccccrrrreeeemmmmeeeennnntttt aaaannnndddd AAAAuuuuttttooooddddeeeeccccrrrreeeemmmmeeeennnntttt
  149.  
  150.           "++" and "--" work as in C.  That is, if placed before a
  151.           variable, they increment or decrement the variable before
  152.           returning the value, and if placed after, increment or
  153.           decrement the variable after returning the value.
  154.  
  155.           The autoincrement operator has a little extra built-in magic
  156.           to it.  If you increment a variable that is numeric, or that
  157.           has ever been used in a numeric context, you get a normal
  158.           increment.  If, however, the variable has only been used in
  159.           string contexts since it was set, and has a value that is
  160.           not null and matches the pattern /^[a-zA-Z]*[0-9]*$/, the
  161.           increment is done as a string, preserving each character
  162.           within its range, with carry:
  163.  
  164.               print ++($foo = '99');      # prints '100'
  165.               print ++($foo = 'a0');      # prints 'a1'
  166.               print ++($foo = 'Az');      # prints 'Ba'
  167.               print ++($foo = 'zz');      # prints 'aaa'
  168.  
  169.           The autodecrement operator is not magical.
  170.  
  171.           EEEExxxxppppoooonnnneeeennnnttttiiiiaaaattttiiiioooonnnn
  172.  
  173.           Binary "**" is the exponentiation operator.  Note that it
  174.           binds even more tightly than unary minus, so -2**4 is
  175.           -(2**4), not (-2)**4.
  176.  
  177.           SSSSyyyymmmmbbbboooolllliiiicccc UUUUnnnnaaaarrrryyyy OOOOppppeeeerrrraaaattttoooorrrrssss
  178.  
  179.           Unary "!" performs logical negation, i.e. "not".  See also
  180.           not for a lower precedence version of this.
  181.  
  182.           Unary "-" performs arithmetic negation if the operand is
  183.           numeric.  If the operand is an identifier, a string
  184.           consisting of a minus sign concatenated with the identifier
  185.           is returned.  Otherwise, if the string starts with a plus or
  186.           minus, a string starting with the opposite sign is returned.
  187.           One effect of these rules is that -bareword is equivalent to
  188.           "-bareword".
  189.  
  190.           Unary "~" performs bitwise negation, i.e. 1's complement.
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  203.  
  204.  
  205.  
  206.           Unary "+" has no effect whatsoever, even on strings.  It is
  207.           useful syntactically for separating a function name from a
  208.           parenthesized expression that would otherwise be interpreted
  209.           as the complete list of function arguments.  (See examples
  210.           above under the section on _L_i_s_t _O_p_e_r_a_t_o_r_s.)
  211.  
  212.           Unary "\" creates a reference to whatever follows it.  See
  213.           the _p_e_r_l_r_e_f manpage.  Do not confuse this behavior with the
  214.           behavior of backslash within a string, although both forms
  215.           do convey the notion of protecting the next thing from
  216.           interpretation.
  217.  
  218.           BBBBiiiinnnnddddiiiinnnngggg OOOOppppeeeerrrraaaattttoooorrrrssss
  219.  
  220.           Binary "=~" binds an expression to a pattern match.  Certain
  221.           operations search or modify the string $_ by default.  This
  222.           operator makes that kind of operation work on some other
  223.           string.  The right argument is a search pattern,
  224.           substitution, or translation.  The left argument is what is
  225.           supposed to be searched, substituted, or translated instead
  226.           of the default $_.  The return value indicates the success
  227.           of the operation.  (If the right argument is an expression
  228.           rather than a search pattern, substitution, or translation,
  229.           it is interpreted as a search pattern at run time.  This is
  230.           less efficient than an explicit search, since the pattern
  231.           must be compiled every time the expression is evaluated--
  232.           unless you've used /o.)
  233.  
  234.           Binary "!~" is just like "=~" except the return value is
  235.           negated in the logical sense.
  236.  
  237.           MMMMuuuullllttttiiiipppplllliiiiccccaaaattttiiiivvvveeee OOOOppppeeeerrrraaaattttoooorrrrssss
  238.  
  239.           Binary "*" multiplies two numbers.
  240.  
  241.           Binary "/" divides two numbers.
  242.  
  243.           Binary "%" computes the modulus of the two numbers.
  244.  
  245.           Binary "x" is the repetition operator.  In a scalar context,
  246.           it returns a string consisting of the left operand repeated
  247.           the number of times specified by the right operand.  In a
  248.           list context, if the left operand is a list in parens, it
  249.           repeats the list.
  250.  
  251.               print '-' x 80;             # print row of dashes
  252.  
  253.               print "\t" x ($tab/8), ' ' x ($tab%8);      # tab over
  254.  
  255.               @ones = (1) x 80;           # a list of 80 1's
  256.               @ones = (5) x @ones;        # set all elements to 5
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  269.  
  270.  
  271.  
  272.           AAAAddddddddiiiittttiiiivvvveeee OOOOppppeeeerrrraaaattttoooorrrrssss
  273.  
  274.           Binary "+" returns the sum of two numbers.
  275.  
  276.           Binary "-" returns the difference of two numbers.
  277.  
  278.           Binary "." concatenates two strings.
  279.  
  280.           SSSShhhhiiiifffftttt OOOOppppeeeerrrraaaattttoooorrrrssss
  281.  
  282.           Binary "<<" returns the value of its left argument shifted
  283.           left by the number of bits specified by the right argument.
  284.           Arguments should be integers.
  285.  
  286.           Binary ">>" returns the value of its left argument shifted
  287.           right by the number of bits specified by the right argument.
  288.           Arguments should be integers.
  289.  
  290.           NNNNaaaammmmeeeedddd UUUUnnnnaaaarrrryyyy OOOOppppeeeerrrraaaattttoooorrrrssss
  291.  
  292.           The various named unary operators are treated as functions
  293.           with one argument, with optional parentheses.  These include
  294.           the filetest operators, like -f, -M, etc.  See the _p_e_r_l_f_u_n_c
  295.           manpage.
  296.  
  297.           If any list operator (_p_r_i_n_t(), etc.) or any unary operator
  298.           (_c_h_d_i_r(), etc.) is followed by a left parenthesis as the
  299.           next token, the operator and arguments within parentheses
  300.           are taken to be of highest precedence, just like a normal
  301.           function call.  Examples:
  302.  
  303.               chdir $foo    || die;       # (chdir $foo) || die
  304.               chdir($foo)   || die;       # (chdir $foo) || die
  305.               chdir ($foo)  || die;       # (chdir $foo) || die
  306.               chdir +($foo) || die;       # (chdir $foo) || die
  307.  
  308.           but, because * is higher precedence than ||:
  309.  
  310.               chdir $foo * 20;    # chdir ($foo * 20)
  311.               chdir($foo) * 20;   # (chdir $foo) * 20
  312.               chdir ($foo) * 20;  # (chdir $foo) * 20
  313.               chdir +($foo) * 20; # chdir ($foo * 20)
  314.  
  315.               rand 10 * 20;       # rand (10 * 20)
  316.               rand(10) * 20;      # (rand 10) * 20
  317.               rand (10) * 20;     # (rand 10) * 20
  318.               rand +(10) * 20;    # rand (10 * 20)
  319.  
  320.           See also the section on _L_i_s_t _O_p_e_r_a_t_o_r_s.
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  335.  
  336.  
  337.  
  338.           RRRReeeellllaaaattttiiiioooonnnnaaaallll OOOOppppeeeerrrraaaattttoooorrrrssss
  339.  
  340.           Binary "<" returns true if the left argument is numerically
  341.           less than the right argument.
  342.  
  343.           Binary ">" returns true if the left argument is numerically
  344.           greater than the right argument.
  345.  
  346.           Binary "<=" returns true if the left argument is numerically
  347.           less than or equal to the right argument.
  348.  
  349.           Binary ">=" returns true if the left argument is numerically
  350.           greater than or equal to the right argument.
  351.  
  352.           Binary "lt" returns true if the left argument is stringwise
  353.           less than the right argument.
  354.  
  355.           Binary "gt" returns true if the left argument is stringwise
  356.           greater than the right argument.
  357.  
  358.           Binary "le" returns true if the left argument is stringwise
  359.           less than or equal to the right argument.
  360.  
  361.           Binary "ge" returns true if the left argument is stringwise
  362.           greater than or equal to the right argument.
  363.  
  364.           EEEEqqqquuuuaaaalllliiiittttyyyy OOOOppppeeeerrrraaaattttoooorrrrssss
  365.  
  366.           Binary "==" returns true if the left argument is numerically
  367.           equal to the right argument.
  368.  
  369.           Binary "!=" returns true if the left argument is numerically
  370.           not equal to the right argument.
  371.  
  372.           Binary "<=>" returns -1, 0, or 1 depending on whether the
  373.           left argument is numerically less than, equal to, or greater
  374.           than the right argument.
  375.  
  376.           Binary "eq" returns true if the left argument is stringwise
  377.           equal to the right argument.
  378.  
  379.           Binary "ne" returns true if the left argument is stringwise
  380.           not equal to the right argument.
  381.  
  382.           Binary "cmp" returns -1, 0, or 1 depending on whether the
  383.           left argument is stringwise less than, equal to, or greater
  384.           than the right argument.
  385.  
  386.           BBBBiiiittttwwwwiiiisssseeee AAAAnnnndddd
  387.  
  388.           Binary "&" returns its operators ANDed together bit by bit.
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  401.  
  402.  
  403.  
  404.           BBBBiiiittttwwwwiiiisssseeee OOOOrrrr aaaannnndddd EEEExxxxcccclllluuuussssiiiivvvveeee OOOOrrrr
  405.  
  406.           Binary "|" returns its operators ORed together bit by bit.
  407.  
  408.           Binary "^" returns its operators XORed together bit by bit.
  409.  
  410.           CCCC----ssssttttyyyylllleeee LLLLooooggggiiiiccccaaaallll AAAAnnnndddd
  411.  
  412.           Binary "&&" performs a short-circuit logical AND operation.
  413.           That is, if the left operand is false, the right operand is
  414.           not even evaluated.  Scalar or list context propagates down
  415.           to the right operand if it is evaluated.
  416.  
  417.           CCCC----ssssttttyyyylllleeee LLLLooooggggiiiiccccaaaallll OOOOrrrr
  418.  
  419.           Binary "||" performs a short-circuit logical OR operation.
  420.           That is, if the left operand is true, the right operand is
  421.           not even evaluated.  Scalar or list context propagates down
  422.           to the right operand if it is evaluated.
  423.  
  424.           The || and && operators differ from C's in that, rather than
  425.           returning 0 or 1, they return the last value evaluated.
  426.           Thus, a reasonably portable way to find out the home
  427.           directory (assuming it's not "0") might be:
  428.  
  429.               $home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
  430.                   (getpwuid($<))[7] || die "You're homeless!\n";
  431.  
  432.           As more readable alternatives to && and ||, Perl provides
  433.           "and" and "or" operators (see below).  The short-circuit
  434.           behavior is identical.  The precedence of "and" and "or" is
  435.           much lower, however, so that you can safely use them after a
  436.           list operator without the need for parentheses:
  437.  
  438.               unlink "alpha", "beta", "gamma"
  439.                       or gripe(), next LINE;
  440.  
  441.           With the C-style operators that would have been written like
  442.           this:
  443.  
  444.               unlink("alpha", "beta", "gamma")
  445.                       || (gripe(), next LINE);
  446.  
  447.  
  448.           RRRRaaaannnnggggeeee OOOOppppeeeerrrraaaattttoooorrrr
  449.  
  450.           Binary ".." is the range operator, which is really two
  451.           different operators depending on the context.  In a list
  452.           context, it returns an array of values counting (by ones)
  453.           from the left value to the right value.  This is useful for
  454.           writing for (1..10) loops and for doing slice operations on
  455.           arrays.  Be aware that under the current implementation, a
  456.  
  457.  
  458.  
  459.      Page 7                                          (printed 6/30/95)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  467.  
  468.  
  469.  
  470.           temporary array is created, so you'll burn a lot of memory
  471.           if you write something like this:
  472.  
  473.               for (1 .. 1_000_000) {
  474.                   # code
  475.               }
  476.  
  477.           In a scalar context, ".." returns a boolean value.  The
  478.           operator is bistable, like a flip-flop, and emulates the
  479.           line-range (comma) operator of sssseeeedddd, aaaawwwwkkkk, and various
  480.           editors.  Each ".." operator maintains its own boolean
  481.           state.  It is false as long as its left operand is false.
  482.           Once the left operand is true, the range operator stays true
  483.           until the right operand is true, _A_F_T_E_R which the range
  484.           operator becomes false again.  (It doesn't become false till
  485.           the next time the range operator is evaluated.  It can test
  486.           the right operand and become false on the same evaluation it
  487.           became true (as in aaaawwwwkkkk), but it still returns true once.  If
  488.           you don't want it to test the right operand till the next
  489.           evaluation (as in sssseeeedddd), use three dots ("...") instead of
  490.           two.)  The right operand is not evaluated while the operator
  491.           is in the "false" state, and the left operand is not
  492.           evaluated while the operator is in the "true" state.  The
  493.           precedence is a little lower than || and &&.  The value
  494.           returned is either the null string for false, or a sequence
  495.           number (beginning with 1) for true.  The sequence number is
  496.           reset for each range encountered.  The final sequence number
  497.           in a range has the string "E0" appended to it, which doesn't
  498.           affect its numeric value, but gives you something to search
  499.           for if you want to exclude the endpoint.  You can exclude
  500.           the beginning point by waiting for the sequence number to be
  501.           greater than 1.  If either operand of scalar ".." is a
  502.           numeric literal, that operand is implicitly compared to the
  503.           $. variable, the current line number.  Examples:
  504.  
  505.           As a scalar operator:
  506.  
  507.               if (101 .. 200) { print; }  # print 2nd hundred lines
  508.               next line if (1 .. /^$/);   # skip header lines
  509.               s/^/> / if (/^$/ .. eof()); # quote body
  510.  
  511.           As a list operator:
  512.  
  513.               for (101 .. 200) { print; } # print $_ 100 times
  514.               @foo = @foo[$[ .. $#foo];   # an expensive no-op
  515.               @foo = @foo[$#foo-4 .. $#foo];      # slice last 5 items
  516.  
  517.           The range operator (in a list context) makes use of the
  518.           magical autoincrement algorithm if the operaands are
  519.           strings.  You can say
  520.  
  521.               @alphabet = ('A' .. 'Z');
  522.  
  523.  
  524.  
  525.      Page 8                                          (printed 6/30/95)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  533.  
  534.  
  535.  
  536.           to get all the letters of the alphabet, or
  537.  
  538.               $hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15];
  539.  
  540.           to get a hexadecimal digit, or
  541.  
  542.               @z2 = ('01' .. '31');  print $z2[$mday];
  543.  
  544.           to get dates with leading zeros.  If the final value
  545.           specified is not in the sequence that the magical increment
  546.           would produce, the sequence goes until the next value would
  547.           be longer than the final value specified.
  548.  
  549.           CCCCoooonnnnddddiiiittttiiiioooonnnnaaaallll OOOOppppeeeerrrraaaattttoooorrrr
  550.  
  551.           Ternary "?:" is the conditional operator, just as in C.  It
  552.           works much like an if-then-else.  If the argument before the
  553.           ? is true, the argument before the : is returned, otherwise
  554.           the argument after the : is returned.  Scalar or list
  555.           context propagates downward into the 2nd or 3rd argument,
  556.           whichever is selected.  The operator may be assigned to if
  557.           both the 2nd and 3rd arguments are legal lvalues (meaning
  558.           that you can assign to them):
  559.  
  560.               ($a_or_b ? $a : $b) = $c;
  561.  
  562.           Note that this is not guaranteed to contribute to the
  563.           readability of your program.
  564.  
  565.           AAAAssssssssiiiiggggmmmmeeeennnntttt OOOOppppeeeerrrraaaattttoooorrrrssss
  566.  
  567.           "=" is the ordinary assignment operator.
  568.  
  569.           Assignment operators work as in C.  That is,
  570.  
  571.               $a += 2;
  572.  
  573.           is equivalent to
  574.  
  575.               $a = $a + 2;
  576.  
  577.           although without duplicating any side effects that
  578.           dereferencing the lvalue might trigger, such as from _t_i_e().
  579.           Other assignment operators work similarly. The following are
  580.           recognized:
  581.  
  582.               **=    +=    *=    &=    <<=    &&=
  583.                      -=    /=    |=    >>=    ||=
  584.                      .=    %=    ^=
  585.                            x=
  586.  
  587.           Note that while these are grouped by family, they all have
  588.  
  589.  
  590.  
  591.      Page 9                                          (printed 6/30/95)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  599.  
  600.  
  601.  
  602.           the precedence of assignment.
  603.  
  604.           Unlike in C, the assignment operator produces a valid
  605.           lvalue.  Modifying an assignment is equivalent to doing the
  606.           assignment and then modifying the variable that was assigned
  607.           to.  This is useful for modifying a copy of something, like
  608.           this:
  609.  
  610.               ($tmp = $global) =~ tr [A-Z] [a-z];
  611.  
  612.           Likewise,
  613.  
  614.               ($a += 2) *= 3;
  615.  
  616.           is equivalent to
  617.  
  618.               $a += 2;
  619.               $a *= 3;
  620.  
  621.  
  622.           Binary "," is the comma operator.  In a scalar context it
  623.           evaluates its left argument, throws that value away, then
  624.           evaluates its right argument and returns that value.  This
  625.           is just like C's comma operator.
  626.  
  627.           In a list context, it's just the list argument separator,
  628.           and inserts both its arguments into the list.
  629.  
  630.           LLLLiiiisssstttt OOOOppppeeeerrrraaaattttoooorrrrssss ((((RRRRiiiigggghhhhttttwwwwaaaarrrrdddd))))
  631.  
  632.           On the right side of a list operator, it has very low
  633.           precedence, such that it controls all comma-separated
  634.           expressions found there.  The only operators with lower
  635.           precedence are the logical operators "and", "or", and "not",
  636.           which may be used to evaluate calls to list operators
  637.           without the need for extra parentheses:
  638.  
  639.               open HANDLE, "filename"
  640.                   or die "Can't open: $!\n";
  641.  
  642.           See also discussion of list operators in the section on _L_i_s_t
  643.           _O_p_e_r_a_t_o_r_s (_L_e_f_t_w_a_r_d).
  644.  
  645.           LLLLooooggggiiiiccccaaaallll NNNNooootttt
  646.  
  647.           Unary "not" returns the logical negation of the expression
  648.           to its right.  It's the equivalent of "!" except for the
  649.           very low precedence.
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.      Page 10                                         (printed 6/30/95)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  665.  
  666.  
  667.  
  668.           LLLLooooggggiiiiccccaaaallll AAAAnnnndddd
  669.  
  670.           Binary "and" returns the logical conjunction of the two
  671.           surrounding expressions.  It's equivalent to && except for
  672.           the very low precedence.  This means that it short-circuits:
  673.           i.e. the right expression is evaluated only if the left
  674.           expression is true.
  675.  
  676.           LLLLooooggggiiiiccccaaaallll oooorrrr aaaannnndddd EEEExxxxcccclllluuuussssiiiivvvveeee OOOOrrrr
  677.  
  678.           Binary "or" returns the logical disjunction of the two
  679.           surrounding expressions.  It's equivalent to || except for
  680.           the very low precedence.  This means that it short-circuits:
  681.           i.e. the right expression is evaluated only if the left
  682.           expression is false.
  683.  
  684.           Binary "xor" returns the exclusive-OR of the two surrounding
  685.           expressions.  It cannot short circuit, of course.
  686.  
  687.           CCCC OOOOppppeeeerrrraaaattttoooorrrrssss MMMMiiiissssssssiiiinnnngggg FFFFrrrroooommmm PPPPeeeerrrrllll
  688.  
  689.           Here is what C has that Perl doesn't:
  690.  
  691.           unary & Address-of operator.  (But see the "\" operator for
  692.                   taking a reference.)
  693.  
  694.           unary * Dereference-address operator. (Perl's prefix
  695.                   dereferencing operators are typed: $, @, %, and &.)
  696.  
  697.           (TYPE)  Type casting operator.
  698.  
  699.           QQQQuuuuooootttteeee aaaannnndddd QQQQuuuuooootttteeeelllliiiikkkkeeee OOOOppppeeeerrrraaaattttoooorrrrssss
  700.  
  701.           While we usually think of quotes as literal values, in Perl
  702.           they function as operators, providing various kinds of
  703.           interpolating and pattern matching capabilities.  Perl
  704.           provides customary quote characters for these behaviors, but
  705.           also provides a way for you to choose your quote character
  706.           for any of them.  In the following table, a {} represents
  707.           any pair of delimiters you choose.  Non-bracketing
  708.           delimiters use the same character fore and aft, but the 4
  709.           sorts of brackets (round, angle, square, curly) will all
  710.           nest.
  711.  
  712.               Customary  Generic     Meaning    Interpolates
  713.                   ''       q{}       Literal         no
  714.                   ""      qq{}       Literal         yes
  715.                   ``      qx{}       Command         yes
  716.                           qw{}      Word list        no
  717.                   //       m{}    Pattern match      yes
  718.                            s{}{}   Substitution      yes
  719.                           tr{}{}   Translation       no
  720.  
  721.  
  722.  
  723.      Page 11                                         (printed 6/30/95)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  731.  
  732.  
  733.  
  734.           For constructs that do interpolation, variables beginning
  735.           with "$ or "@" are interpolated, as are the following
  736.           sequences:
  737.  
  738.               \t          tab
  739.               \n          newline
  740.               \r          return
  741.               \f          form feed
  742.               \v          vertical tab, whatever that is
  743.               \b          backspace
  744.               \a          alarm (bell)
  745.               \e          escape
  746.               \033        octal char
  747.               \x1b        hex char
  748.               \c[         control char
  749.               \l          lowercase next char
  750.               \u          uppercase next char
  751.               \L          lowercase till \E
  752.               \U          uppercase till \E
  753.               \E          end case modification
  754.               \Q          quote regexp metacharacters till \E
  755.  
  756.           Patterns are subject to an additional level of
  757.           interpretation as a regular expression.  This is done as a
  758.           second pass, after variables are interpolated, so that
  759.           regular expressions may be incorporated into the pattern
  760.           from the variables.  If this is not what you want, use \Q to
  761.           interpolate a variable literally.
  762.  
  763.           Apart from the above, there are no multiple levels of
  764.           interpolation.  In particular, contrary to the expectations
  765.           of shell programmers, backquotes do _N_O_T interpolate within
  766.           double quotes, nor do single quotes impede evaluation of
  767.           variables when used within double quotes.
  768.  
  769.           ?PATTERN?
  770.                   This is just like the /pattern/ search, except that
  771.                   it matches only once between calls to the _r_e_s_e_t()
  772.                   operator.  This is a useful optimization when you
  773.                   only want to see the first occurrence of something
  774.                   in each file of a set of files, for instance.  Only
  775.                   ??  patterns local to the current package are reset.
  776.  
  777.                   This usage is vaguely deprecated, and may be removed
  778.                   in some future version of Perl.
  779.  
  780.           m/PATTERN/gimosx
  781.  
  782.           /PATTERN/gimosx
  783.                   Searches a string for a pattern match, and in a
  784.                   scalar context returns true (1) or false ('').  If
  785.                   no string is specified via the =~ or !~ operator,
  786.  
  787.  
  788.  
  789.      Page 12                                         (printed 6/30/95)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  797.  
  798.  
  799.  
  800.                   the $_ string is searched.  (The string specified
  801.                   with =~ need not be an lvalue--it may be the result
  802.                   of an expression evaluation, but remember the =~
  803.                   binds rather tightly.)  See also the _p_e_r_l_r_e manpage.
  804.  
  805.                   Options are:
  806.  
  807.                       g   Match globally, i.e. find all occurrences.
  808.                       i   Do case-insensitive pattern matching.
  809.                       m   Treat string as multiple lines.
  810.                       o   Only compile pattern once.
  811.                       s   Treat string as single line.
  812.                       x   Use extended regular expressions.
  813.  
  814.                   If "/" is the delimiter then the initial m is
  815.                   optional.  With the m you can use any pair of non-
  816.                   alphanumeric, non-whitespace characters as
  817.                   delimiters.  This is particularly useful for
  818.                   matching Unix path names that contain "/", to avoid
  819.                   LTS (leaning toothpick syndrome).
  820.  
  821.                   PATTERN may contain variables, which will be
  822.                   interpolated (and the pattern recompiled) every time
  823.                   the pattern search is evaluated.  (Note that $) and
  824.                   $| might not be interpolated because they look like
  825.                   end-of-string tests.)  If you want such a pattern to
  826.                   be compiled only once, add a /o after the trailing
  827.                   delimiter.  This avoids expensive run-time
  828.                   recompilations, and is useful when the value you are
  829.                   interpolating won't change over the life of the
  830.                   script.  However, mentioning /o constitutes a
  831.                   promise that you won't change the variables in the
  832.                   pattern.  If you change them, Perl won't even
  833.                   notice.
  834.  
  835.                   If the PATTERN evaluates to a null string, the most
  836.                   recently executed (and successfully compiled)
  837.                   regular expression is used instead.
  838.  
  839.                   If used in a context that requires a list value, a
  840.                   pattern match returns a list consisting of the
  841.                   subexpressions matched by the parentheses in the
  842.                   pattern, i.e. ($1, $2, $3...).  (Note that here $1
  843.                   etc. are also set, and that this differs from Perl
  844.                   4's behavior.)  If the match fails, a null array is
  845.                   returned.  If the match succeeds, but there were no
  846.                   parentheses, a list value of (1) is returned.
  847.  
  848.                   Examples:
  849.  
  850.                       open(TTY, '/dev/tty');
  851.                       <TTY> =~ /^y/i && foo();    # do foo if desired
  852.  
  853.  
  854.  
  855.      Page 13                                         (printed 6/30/95)
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  863.  
  864.  
  865.  
  866.                       if (/Version: *([0-9.]*)/) { $version = $1; }
  867.  
  868.                       next if m#^/usr/spool/uucp#;
  869.  
  870.                       # poor man's grep
  871.                       $arg = shift;
  872.                       while (<>) {
  873.                           print if /$arg/o;       # compile only once
  874.                       }
  875.  
  876.                       if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
  877.  
  878.                   This last example splits $foo into the first two
  879.                   words and the remainder of the line, and assigns
  880.                   those three fields to $F1, $F2 and $Etc.  The
  881.                   conditional is true if any variables were assigned,
  882.                   i.e. if the pattern matched.
  883.  
  884.                   The /g modifier specifies global pattern matching--
  885.                   that is, matching as many times as possible within
  886.                   the string.  How it behaves depends on the context.
  887.                   In a list context, it returns a list of all the
  888.                   substrings matched by all the parentheses in the
  889.                   regular expression.  If there are no parentheses, it
  890.                   returns a list of all the matched strings, as if
  891.                   there were parentheses around the whole pattern.
  892.  
  893.                   In a scalar context, m//g iterates through the
  894.                   string, returning TRUE each time it matches, and
  895.                   FALSE when it eventually runs out of matches.  (In
  896.                   other words, it remembers where it left off last
  897.                   time and restarts the search at that point.  You can
  898.                   actually find the current match position of a string
  899.                   using the _p_o_s() function--see the _p_e_r_l_f_u_n_c manpage.)
  900.                   If you modify the string in any way, the match
  901.                   position is reset to the beginning.  Examples:
  902.  
  903.                       # list context
  904.                       ($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g);
  905.  
  906.                       # scalar context
  907.                       $/ = ""; $* = 1;  # $* deprecated in Perl 5
  908.                       while ($paragraph = <>) {
  909.                           while ($paragraph =~ /[a-z]['")]*[.!?]+['")]*\s/g) {
  910.                               $sentences++;
  911.                           }
  912.                       }
  913.                       print "$sentences\n";
  914.  
  915.  
  916.           q/STRING/
  917.  
  918.  
  919.  
  920.  
  921.      Page 14                                         (printed 6/30/95)
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  929.  
  930.  
  931.  
  932.           'STRING'
  933.                   A single-quoted, literal string.  Backslashes are
  934.                   ignored, unless followed by the delimiter or another
  935.                   backslash, in which case the delimiter or backslash
  936.                   is interpolated.
  937.  
  938.                       $foo = q!I said, "You said, 'She said it.'"!;
  939.                       $bar = q('This is it.');
  940.  
  941.  
  942.           qq/STRING/
  943.  
  944.                   A double-quoted, interpolated string.
  945.  
  946.                       $_ .= qq
  947.                        (*** The previous line contains the naughty word "$1".\n)
  948.                                   if /(tcl|rexx|python)/;      # :-)
  949.  
  950.  
  951.           qx/STRING/
  952.  
  953.           `STRING`
  954.                   A string which is interpolated and then executed as
  955.                   a system command.  The collected standard output of
  956.                   the command is returned.  In scalar context, it
  957.                   comes back as a single (potentially multi-line)
  958.                   string.  In list context, returns a list of lines
  959.                   (however you've defined lines with $/ or
  960.                   $INPUT_RECORD_SEPARATOR).
  961.  
  962.                       $today = qx{ date };
  963.  
  964.                   See the section on _I/_O _O_p_e_r_a_t_o_r_s for more
  965.                   discussion.
  966.  
  967.           qw/STRING/
  968.                   Returns a list of the words extracted out of STRING,
  969.                   using embedded whitespace as the word delimiters.
  970.                   It is exactly equivalent to
  971.  
  972.                       split(' ', q/STRING/);
  973.  
  974.                   Some frequently seen examples:
  975.  
  976.                       use POSIX qw( setlocale localeconv )
  977.                       @EXPORT = qw( foo bar baz );
  978.  
  979.  
  980.           s/PATTERN/REPLACEMENT/egimosx
  981.                   Searches a string for a pattern, and if found,
  982.                   replaces that pattern with the replacement text and
  983.                   returns the number of substitutions made.  Otherwise
  984.  
  985.  
  986.  
  987.      Page 15                                         (printed 6/30/95)
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  995.  
  996.  
  997.  
  998.                   it returns false (0).
  999.  
  1000.                   If no string is specified via the =~ or !~ operator,
  1001.                   the $_ variable is searched and modified.  (The
  1002.                   string specified with =~ must be a scalar variable,
  1003.                   an array element, a hash element, or an assignment
  1004.                   to one of those, i.e. an lvalue.)
  1005.  
  1006.                   If the delimiter chosen is single quote, no variable
  1007.                   interpolation is done on either the PATTERN or the
  1008.                   REPLACEMENT.  Otherwise, if the PATTERN contains a $
  1009.                   that looks like a variable rather than an end-of-
  1010.                   string test, the variable will be interpolated into
  1011.                   the pattern at run-time.  If you only want the
  1012.                   pattern compiled once the first time the variable is
  1013.                   interpolated, use the /o option.  If the pattern
  1014.                   evaluates to a null string, the most recently
  1015.                   executed (and successfully compiled) regular
  1016.                   expression is used instead.  See the _p_e_r_l_r_e manpage
  1017.                   for further explanation on these.
  1018.  
  1019.                   Options are:
  1020.  
  1021.                       e   Evaluate the right side as an expression.
  1022.                       g   Replace globally, i.e. all occurrences.
  1023.                       i   Do case-insensitive pattern matching.
  1024.                       m   Treat string as multiple lines.
  1025.                       o   Only compile pattern once.
  1026.                       s   Treat string as single line.
  1027.                       x   Use extended regular expressions.
  1028.  
  1029.                   Any non-alphanumeric, non-whitespace delimiter may
  1030.                   replace the slashes.  If single quotes are used, no
  1031.                   interpretation is done on the replacement string
  1032.                   (the /e modifier overrides this, however).  If
  1033.                   backquotes are used, the replacement string is a
  1034.                   command to execute whose output will be used as the
  1035.                   actual replacement text.  If the PATTERN is
  1036.                   delimited by bracketing quotes, the REPLACEMENT has
  1037.                   its own pair of quotes, which may or may not be
  1038.                   bracketing quotes, e.g.  s(foo)(bar) or s<foo>/bar/.
  1039.                   A /e will cause the replacement portion to be
  1040.                   interpreter as a full-fledged Perl expression and
  1041.                   _e_v_a_l()ed right then and there.  It is, however,
  1042.                   syntax checked at compile-time.
  1043.  
  1044.                   Examples:
  1045.  
  1046.                       s/\bgreen\b/mauve/g;                # don't change wintergreen
  1047.  
  1048.                       $path =~ s|/usr/bin|/usr/local/bin|;
  1049.  
  1050.  
  1051.  
  1052.  
  1053.      Page 16                                         (printed 6/30/95)
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1061.  
  1062.  
  1063.  
  1064.                       s/Login: $foo/Login: $bar/; # run-time pattern
  1065.  
  1066.                       ($foo = $bar) =~ s/this/that/;
  1067.  
  1068.                       $count = ($paragraph =~ s/Mister\b/Mr./g);
  1069.  
  1070.                       $_ = 'abc123xyz';
  1071.                       s/\d+/$&*2/e;               # yields 'abc246xyz'
  1072.                       s/\d+/sprintf("%5d",$&)/e;  # yields 'abc  246xyz'
  1073.                       s/\w/$& x 2/eg;             # yields 'aabbcc  224466xxyyzz'
  1074.  
  1075.                       s/%(.)/$percent{$1}/g;      # change percent escapes; no /e
  1076.                       s/%(.)/$percent{$1} || $&/ge;       # expr now, so /e
  1077.                       s/^=(\w+)/&pod($1)/ge;      # use function call
  1078.  
  1079.                       # /e's can even nest;  this will expand
  1080.                       # simple embedded variables in $_
  1081.                       s/(\$\w+)/$1/eeg;
  1082.  
  1083.                       # Delete C comments.
  1084.                       $program =~ s {
  1085.                           /\*     (?# Match the opening delimiter.)
  1086.                           .*?     (?# Match a minimal number of characters.)
  1087.                           \*/     (?# Match the closing delimiter.)
  1088.                       } []gsx;
  1089.  
  1090.                       s/^\s*(.*?)\s*$/$1/;        # trim white space
  1091.  
  1092.                       s/([^ ]*) *([^ ]*)/$2 $1/;  # reverse 1st two fields
  1093.  
  1094.                   Note the use of $ instead of \ in the last example.
  1095.                   Unlike sssseeeedddd, we only use the \<_d_i_g_i_t> form in the
  1096.                   left hand side.  Anywhere else it's $<_d_i_g_i_t>.
  1097.  
  1098.                   Occasionally, you can't just use a /g to get all the
  1099.                   changes to occur.  Here are two common cases:
  1100.  
  1101.                       # put commas in the right places in an integer
  1102.                       1 while s/(.*\d)(\d\d\d)/$1,$2/g;      # perl4
  1103.                       1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;  # perl5
  1104.  
  1105.                       # expand tabs to 8-column spacing
  1106.                       1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
  1107.  
  1108.  
  1109.           tr/SEARCHLIST/REPLACEMENTLIST/cds
  1110.  
  1111.           y/SEARCHLIST/REPLACEMENTLIST/cds
  1112.                   Translates all occurrences of the characters found
  1113.                   in the search list with the corresponding character
  1114.                   in the replacement list.  It returns the number of
  1115.                   characters replaced or deleted.  If no string is
  1116.  
  1117.  
  1118.  
  1119.      Page 17                                         (printed 6/30/95)
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1127.  
  1128.  
  1129.  
  1130.                   specified via the =~ or !~ operator, the $_ string
  1131.                   is translated.  (The string specified with =~ must
  1132.                   be a scalar variable, an array element, or an
  1133.                   assignment to one of those, i.e. an lvalue.)  For
  1134.                   sssseeeedddd devotees, y is provided as a synonym for tr.  If
  1135.                   the SEARCHLIST is delimited by bracketing quotes,
  1136.                   the REPLACEMENTLIST has its own pair of quotes,
  1137.                   which may or may not be bracketing quotes, e.g.
  1138.                   tr[A-Z][a-z] or tr(+-*/)/ABCD/.
  1139.  
  1140.                   Options:
  1141.  
  1142.                       c   Complement the SEARCHLIST.
  1143.                       d   Delete found but unreplaced characters.
  1144.                       s   Squash duplicate replaced characters.
  1145.  
  1146.                   If the /c modifier is specified, the SEARCHLIST
  1147.                   character set is complemented.  If the /d modifier
  1148.                   is specified, any characters specified by SEARCHLIST
  1149.                   not found in REPLACEMENTLIST are deleted.  (Note
  1150.                   that this is slightly more flexible than the
  1151.                   behavior of some ttttrrrr programs, which delete anything
  1152.                   they find in the SEARCHLIST, period.) If the /s
  1153.                   modifier is specified, sequences of characters that
  1154.                   were translated to the same character are squashed
  1155.                   down to a single instance of the character.
  1156.  
  1157.                   If the /d modifier is used, the REPLACEMENTLIST is
  1158.                   always interpreted exactly as specified.  Otherwise,
  1159.                   if the REPLACEMENTLIST is shorter than the
  1160.                   SEARCHLIST, the final character is replicated till
  1161.                   it is long enough.  If the REPLACEMENTLIST is null,
  1162.                   the SEARCHLIST is replicated.  This latter is useful
  1163.                   for counting characters in a class or for squashing
  1164.                   character sequences in a class.
  1165.  
  1166.                   Examples:
  1167.  
  1168.                       $ARGV[1] =~ tr/A-Z/a-z/;    # canonicalize to lower case
  1169.  
  1170.                       $cnt = tr/*/*/;             # count the stars in $_
  1171.  
  1172.                       $cnt = $sky =~ tr/*/*/;     # count the stars in $sky
  1173.  
  1174.                       $cnt = tr/0-9//;            # count the digits in $_
  1175.  
  1176.                       tr/a-zA-Z//s;               # bookkeeper -> bokeper
  1177.  
  1178.                       ($HOST = $host) =~ tr/a-z/A-Z/;
  1179.  
  1180.                       tr/a-zA-Z/ /cs;             # change non-alphas to single space
  1181.  
  1182.  
  1183.  
  1184.  
  1185.      Page 18                                         (printed 6/30/95)
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1193.  
  1194.  
  1195.  
  1196.                       tr [\200-\377]
  1197.                          [\000-\177];             # delete 8th bit
  1198.  
  1199.                   Note that because the translation table is built at
  1200.                   compile time, neither the SEARCHLIST nor the
  1201.                   REPLACEMENTLIST are subjected to double quote
  1202.                   interpolation.  That means that if you want to use
  1203.                   variables, you must use an _e_v_a_l():
  1204.  
  1205.                       eval "tr/$oldlist/$newlist/";
  1206.                       die $@ if $@;
  1207.  
  1208.                       eval "tr/$oldlist/$newlist/, 1" or die $@;
  1209.  
  1210.  
  1211.           IIII////OOOO OOOOppppeeeerrrraaaattttoooorrrrssss
  1212.  
  1213.           There are several I/O operators you should know about. A
  1214.           string is enclosed by backticks (grave accents) first
  1215.           undergoes variable substitution just like a double quoted
  1216.           string.  It is then interpreted as a command, and the output
  1217.           of that command is the value of the pseudo-literal, like in
  1218.           a shell.  In a scalar context, a single string consisting of
  1219.           all the output is returned.  In a list context, a list of
  1220.           values is returned, one for each line of output.  (You can
  1221.           set $/ to use a different line terminator.)  The command is
  1222.           executed each time the pseudo-literal is evaluated.  The
  1223.           status value of the command is returned in $? (see the
  1224.           _p_e_r_l_v_a_r manpage for the interpretation of $?).  Unlike in
  1225.           ccccsssshhhh, no translation is done on the return data--newlines
  1226.           remain newlines.  Unlike in any of the shells, single quotes
  1227.           do not hide variable names in the command from
  1228.           interpretation.  To pass a $ through to the shell you need
  1229.           to hide it with a backslash.  The generalized form of
  1230.           backticks is qx//.
  1231.  
  1232.           Evaluating a filehandle in angle brackets yields the next
  1233.           line from that file (newline included, so it's never false
  1234.           until end of file, at which time an undefined value is
  1235.           returned).  Ordinarily you must assign that value to a
  1236.           variable, but there is one situation where an automatic
  1237.           assignment happens.  _I_f _a_n_d _O_N_L_Y _i_f the input symbol is the
  1238.           only thing inside the conditional of a while loop, the value
  1239.           is automatically assigned to the variable $_.  (This may
  1240.           seem like an odd thing to you, but you'll use the construct
  1241.           in almost every Perl script you write.)  Anyway, the
  1242.           following lines are equivalent to each other:
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.      Page 19                                         (printed 6/30/95)
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1259.  
  1260.  
  1261.  
  1262.               while ($_ = <STDIN>) { print; }
  1263.               while (<STDIN>) { print; }
  1264.               for (;<STDIN>;) { print; }
  1265.               print while $_ = <STDIN>;
  1266.               print while <STDIN>;
  1267.  
  1268.           The filehandles STDIN, STDOUT and STDERR are predefined.
  1269.           (The filehandles stdin, stdout and stderr will also work
  1270.           except in packages, where they would be interpreted as local
  1271.           identifiers rather than global.)  Additional filehandles may
  1272.           be created with the _o_p_e_n() function.
  1273.  
  1274.           If a <FILEHANDLE> is used in a context that is looking for a
  1275.           list, a list consisting of all the input lines is returned,
  1276.           one line per list element.  It's easy to make a _L_A_R_G_E data
  1277.           space this way, so use with care.
  1278.  
  1279.           The null filehandle <> is special and can be used to emulate
  1280.           the behavior of sssseeeedddd and aaaawwwwkkkk.  Input from <> comes either
  1281.           from standard input, or from each file listed on the command
  1282.           line.  Here's how it works: the first time <> is evaluated,
  1283.           the @ARGV array is checked, and if it is null, $ARGV[0] is
  1284.           set to "-", which when opened gives you standard input.  The
  1285.           @ARGV array is then processed as a list of filenames.  The
  1286.           loop
  1287.  
  1288.               while (<>) {
  1289.                   ...                     # code for each line
  1290.               }
  1291.  
  1292.           is equivalent to the following Perl-like pseudo code:
  1293.  
  1294.               unshift(@ARGV, '-') if $#ARGV < $[;
  1295.               while ($ARGV = shift) {
  1296.                   open(ARGV, $ARGV);
  1297.                   while (<ARGV>) {
  1298.                       ...         # code for each line
  1299.                   }
  1300.               }
  1301.  
  1302.           except that it isn't so cumbersome to say, and will actually
  1303.           work.  It really does shift array @ARGV and put the current
  1304.           filename into variable $ARGV.  It also uses filehandle _A_R_G_V
  1305.           internally--<> is just a synonym for <ARGV>, which is
  1306.           magical.  (The pseudo code above doesn't work because it
  1307.           treats <ARGV> as non-magical.)
  1308.  
  1309.           You can modify @ARGV before the first <> as long as the
  1310.           array ends up containing the list of filenames you really
  1311.           want.  Line numbers ($.) continue as if the input were one
  1312.           big happy file.  (But see example under _e_o_f() for how to
  1313.           reset line numbers on each file.)
  1314.  
  1315.  
  1316.  
  1317.      Page 20                                         (printed 6/30/95)
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1325.  
  1326.  
  1327.  
  1328.           If you want to set @ARGV to your own list of files, go right
  1329.           ahead.  If you want to pass switches into your script, you
  1330.           can use one of the Getopts modules or put a loop on the
  1331.           front like this:
  1332.  
  1333.               while ($_ = $ARGV[0], /^-/) {
  1334.                   shift;
  1335.                   last if /^--$/;
  1336.                   if (/^-D(.*)/) { $debug = $1 }
  1337.                   if (/^-v/)     { $verbose++  }
  1338.                   ...             # other switches
  1339.               }
  1340.               while (<>) {
  1341.                   ...             # code for each line
  1342.               }
  1343.  
  1344.           The <> symbol will return FALSE only once.  If you call it
  1345.           again after this it will assume you are processing another
  1346.           @ARGV list, and if you haven't set @ARGV, will input from
  1347.           STDIN.
  1348.  
  1349.           If the string inside the angle brackets is a reference to a
  1350.           scalar variable (e.g. <$foo>), then that variable contains
  1351.           the name of the filehandle to input from.
  1352.  
  1353.           If the string inside angle brackets is not a filehandle, it
  1354.           is interpreted as a filename pattern to be globbed, and
  1355.           either a list of filenames or the next filename in the list
  1356.           is returned, depending on context.  One level of $
  1357.           interpretation is done first, but you can't say <$foo>
  1358.           because that's an indirect filehandle as explained in the
  1359.           previous paragraph.  You could insert curly brackets to
  1360.           force interpretation as a filename glob: <${foo}>.
  1361.           (Alternately, you can call the internal function directly as
  1362.           glob($foo), which is probably the right way to have done it
  1363.           in the first place.)  Example:
  1364.  
  1365.               while (<*.c>) {
  1366.                   chmod 0644, $_;
  1367.               }
  1368.  
  1369.           is equivalent to
  1370.  
  1371.               open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
  1372.               while (<FOO>) {
  1373.                   chop;
  1374.                   chmod 0644, $_;
  1375.               }
  1376.  
  1377.           In fact, it's currently implemented that way.  (Which means
  1378.           it will not work on filenames with spaces in them unless you
  1379.           have _c_s_h(1) on your machine.)  Of course, the shortest way
  1380.  
  1381.  
  1382.  
  1383.      Page 21                                         (printed 6/30/95)
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.      PPPPEEEERRRRLLLLOOOOPPPP((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLOOOOPPPP((((1111))))
  1391.  
  1392.  
  1393.  
  1394.           to do the above is:
  1395.  
  1396.               chmod 0644, <*.c>;
  1397.  
  1398.           Because globbing invokes a shell, it's often faster to call
  1399.           _r_e_a_d_d_i_r() yourself and just do your own _g_r_e_p() on the
  1400.           filenames.  Furthermore, due to its current implementation
  1401.           of using a shell, the _g_l_o_b() routine may get "Arg list too
  1402.           long" errors (unless you've installed _t_c_s_h(1L) as /_b_i_n/_c_s_h).
  1403.  
  1404.           CCCCoooonnnnssssttttaaaannnntttt FFFFoooollllddddiiiinnnngggg
  1405.  
  1406.           Like C, Perl does a certain amount of expression evaluation
  1407.           at compile time, whenever it determines that all of the
  1408.           arguments to an operator are static and have no side
  1409.           effects.  In particular, string concatenation happens at
  1410.           compile time between literals that don't do variable
  1411.           substitution.  Backslash interpretation also happens at
  1412.           compile time.  You can say
  1413.  
  1414.               'Now is the time for all' . "\n" .
  1415.                   'good men to come to.'
  1416.  
  1417.           and this all reduces to one string internally.  Likewise, if
  1418.           you say
  1419.  
  1420.               foreach $file (@filenames) {
  1421.                   if (-s $file > 5 + 100 * 2**16) { ... }
  1422.               }
  1423.  
  1424.           the compiler will pre-compute the number that expression
  1425.           represents so that the interpreter won't have to.
  1426.  
  1427.           IIIInnnntttteeeeggggeeeerrrr aaaarrrriiiitttthhhhmmmmeeeettttiiiicccc
  1428.  
  1429.           By default Perl assumes that it must do most of its
  1430.           arithmetic in floating point.  But by saying
  1431.  
  1432.               use integer;
  1433.  
  1434.           you may tell the compiler that it's okay to use integer
  1435.           operations from here to the end of the enclosing BLOCK.  An
  1436.           inner BLOCK may countermand this by saying
  1437.  
  1438.               no integer;
  1439.  
  1440.           which lasts until the end of that BLOCK.
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.      Page 22                                         (printed 6/30/95)
  1450.  
  1451.  
  1452.  
  1453.